package com.txc.books.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.txc.books.entity.FilingRecords;
import com.txc.books.mapper.FilingRecordsMapper;
import com.txc.books.service.IFilingRecordsService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import com.txc.books.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: XCTANG
 *
 */

@Service
public class FilingRecordsImpl implements IFilingRecordsService {

    private FilingRecordsMapper filingRecordsMapper;

    public FilingRecordsImpl(FilingRecordsMapper filingRecordsMapper){
        this.filingRecordsMapper = filingRecordsMapper;
    }

    @Override
    public List<FilingRecords> findAll() {
        return filingRecordsMapper.findAll();
    }

    @Override
    public void insert(FilingRecords filingRecords) {
        String name = filingRecords.getBookName();
        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }
        filingRecordsMapper.insert(filingRecords);
    }

    @Override
    public void update(FilingRecords filingRecords) {

        String name = filingRecords.getBookName();
        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }
        filingRecordsMapper.update(filingRecords);
    }

    @Override
    public void delete(Integer id) {
        filingRecordsMapper.delete(id);
    }

    @Override
    public void select(Integer id) {
        filingRecordsMapper.select(id);
    }

    @Override
    public PageResult findByPager(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    public PageInfo<FilingRecords> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<FilingRecords> filingMenus = filingRecordsMapper.selectPage();
        return new PageInfo<FilingRecords>(filingMenus);
    }
}
